<?php

namespace App\Http\Controllers;

use App\Http\Biz\AccountBiz;
use App\Http\Biz\SuppBiz;
use App\Http\Utils\Common\Consts;
use App\Http\Utils\SmsUtils;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Log;
use App\Http\Utils\Common\OperResult;
use Exception;
use Monolog\Handler\LogglyHandler;

class SuppController extends Controller{

    //验证手机号是否已经存在
    public function validPhoneIsExist(Request $req){
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('phone')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            };
            $phone = $req->input('phone', '');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->validPhoneIsExist($phone);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            if($result==0){
                return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>'电话号码不存在']);
            }else{
                return response()->json(['code'=>OperResult::CODE_DELETE_FAIL, 'msg'=>'电话号码存在']);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //用户注册
    public function register(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if(!$req->has('supp')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $params=$req->input('supp','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->register($params);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            switch ($result['code']){
                case -1:
                    return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                        'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
                    break;
                case -3:
                    return response()->json(['code'=>'-3','msg'=>'手机号已经注册']);
                    break;
                case -4:
                    return response()->json(['code'=>OperResult::CODE_CREATE_FAIL,
                        'msg'=>OperResult::CODE_CREATE_FAIL_DESC]);
                    break;
                case 1:
                    return response()->json(['code'=>OperResult::CODE_CREATE_OK,
                        'msg'=>OperResult::CODE_CREATE_OK_DESC,'data'=>$result['data']]);
                    break;
                default:
                    return response()->json(['code'=>OperResult::CODE_500,
                        'msg'=>OperResult::CODE_500_DESC]);
                    break;
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }

    //登录
    public function login(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if(!$req->has('phone')||!$req->has('psw')||!$req->has('openid')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $phone=$req->input('phone','');
            $psw=$req->input('psw','');
            $openid=$req->input('openid','');
            $avatar=$req->input('avatar','');
            $nick=$req->input('nick','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->login($phone,$psw,$openid,$avatar,$nick);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            switch ($result['code']){
                case 1:
                    return response()->json(['code'=>OperResult::CODE_AUTH_OK,
                        'msg'=>OperResult::CODE_AUTH_OK_DESC,'data'=>$result['data']]);
                    break;
                case -1:
                    return response()->json(['code'=>OperResult::CODE_AUTH_FAIL,
                        'msg'=>OperResult::CODE_AUTH_FAIL_DESC]);
                    break;
                default:
                    return response()->json(['code'=>OperResult::CODE_500,
                    'msg'=>OperResult::CODE_500_DESC]);
                    break;
            }

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }

    //发送验证码
    public function sendSms(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('phone')||!$req->has('openid')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $phone=$req->input('phone','');
            $openid=$req->input('openid','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->sendSms($phone,$openid);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            switch ($result['code']){
                case 1:
                    return response()->json(['code'=>OperResult::CODE_READ_OK,
                        'msg'=>'短信发送成功','data'=>$result['data']]);
                    break;
                case -1:
                    return response()->json(['code'=>-2,
                        'msg'=>'短信发送失败']);
                    break;
                default:
                    return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
                    break;
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //验证码验证
    public function vcodeVerify(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('vcode')||!$req->has('openid')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $vcode=$req->input('vcode','');
            $openid=$req->input('openid','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->vcodeVerify($vcode,$openid);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            switch ($result['code']){
                case 1:
                    return response()->json(['code'=>1,
                        'msg'=>'验证通过']);
                    break;
                case -1:
                    return response()->json(['code'=>-1,
                        'msg'=>'验证码错误']);
                    break;
                case -2:
                    return response()->json(['code'=>-2,
                        'msg'=>'验证码已过期']);
                    break;
                default:
                    return response()->json(['code'=>-1,
                        'msg'=>'验证码错误']);
                    break;
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }

    

    //修改密码
    public function updatePsw(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')||!$req->has('old_psw')||!$req->has('new_psw')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $supp_id=$req->input('supp_id');
            $old_psw=$req->input('old_psw');
            $new_psw=$req->input('new_psw');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->updatePsw($supp_id,$old_psw,$new_psw);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            $code=$result['code'];
            if($code==-1){
                return response()->json(['code'=>'-1',
                    'msg'=>'密码错误']);
            }else{
                return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                    'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //忘记密码
    public function forgetPsw(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('phone')||!$req->has('psw')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $phone=$req->input('phone','');
            $psw=$req->input('psw','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->forgetPsw($phone,$psw);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            switch ($result['code']){
                case -2:
                    return response()->json(['code'=>-2,'msg'=>'账号不存在']);
                    break;
                case 1:
                    return response()->json(['code'=>2,
                        'msg'=>'成功','data'=>$result['data']]);
                    break;
                default:
                    return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
                    break;
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //获取供应商首页数据
    public function getFirstPageMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('unique_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }
            $unique_id=$req->input('unique_id');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->getFirstPageMsg($unique_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }




    //分页获取素材商品列表
    public function readGoodsByCondition(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $where=$req->input('supp',array());
            $supp_id=$req->input('supp_id','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->readGoodsByCondition($supp_id,$where,$req->account_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //根据id获取素材商品详情
    public function readGoodsById(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $id=$req->input('id','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->readGoodsById($id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //修改供应商信息
    public function updateSuppMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp')||!$req->has('supp_id')||!$req->has('phone')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $supp=$req->input('supp','');
            $supp_id=$req->input('supp_id','');
            $phone=$req->input('phone','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->updateSuppMsg($supp,$supp_id,$phone);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            if($result['code']==1){
                return response()->json(['code'=>OperResult::CODE_UPDATE_OK,
                    'msg'=>OperResult::CODE_UPDATE_OK_DESC]);
            }else{
                return response()->json(['code'=>-1,
                    'msg'=>'电话已注册']);
            }
        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }



    //根据supp_id获取供应商详情
    public function readSuppById(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $supp_id=$req->input('supp_id','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->readSuppById($supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');

            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //获取报表信息
    public function readStatMsg(Request $req){
        try{
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');

            if(!$req->has('supp_id')){
                return response()->json(['code'=>OperResult::CODE_PARAM_MISSING,
                    'msg'=>OperResult::CODE_PARAM_MISSING_DESC]);
            }

            $supp_id=$req->input('supp_id','');
            $suppBiz=new SuppBiz();
            $result=$suppBiz->readStatMsg($supp_id);

            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK,
                'msg'=>OperResult::CODE_READ_OK_DESC,'data'=>$result]);

        }catch (Exception $ex){
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_500, 'msg'=>OperResult::CODE_500_DESC]);
        }
    }


    //我管理的供应商列表
    public function getSuppList(Request $request)
    {
        try {
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'start');
            if(!$request->has('account_id') || !$request->has('supp_id')){
                return response()->json(['code' => OperResult::CODE_DELETE_FAIL,
                    'msg' => OperResult::CODE_CREATE_FAIL_DESC, 'data' => array()]);
            }
            $suppBiz=new SuppBiz();
            $data = $suppBiz->getShopListBiz($request->account_id,$request->supp_id);
            Log::debug(get_class($this) . ' ' . __FUNCTION__ . ' ' . 'end');
            return response()->json(['code' => OperResult::CODE_READ_OK,
                'msg' => OperResult::CODE_READ_OK_DESC, 'data' => $data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL,
                'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }
    }

    public function getOpenid(Request $request) {
        try {
            $code = $request->code;
            $client = new Client();
            $res = $client->request('GET', 'https://api.weixin.qq.com/sns/jscode2session?appid='.Consts::WX_APPID.'&secret='.Consts::WX_SECRET.'&js_code='.$code.'&grant_type=authorization_code');
            $json = $res->getBody();
            return $json;
        } catch (ErrorException $ex) {
            Log::error(get_class($this) . ' exception ' . print_r($ex->getMessage(), true));
            return response()->json(['code' => OperResult::CODE_READ_FAIL, 'msg' => OperResult::CODE_READ_FAIL_DESC, 'data' => array()]);
        }

    }

    /**
     * @param Request $req
     * @return mixed
     */
    public function prov_list(Request $req) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            $flag = $req->has('flag') ? $req->input('flag',0) : 0;
            $suppBiz=new SuppBiz();
            $data = $suppBiz->prov_listBiz($flag);
//            $data = PtProvCriteria::build_prov_all($flag);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>2, 'msg'=>'读取成功', 'data'=>$data]);
        } catch (PDOException $pdo_ex) {
            Log::error(get_class($this).' exception '.print_r($pdo_ex->getMessage(), true));
            return response()->json(['code'=>-2, 'msg'=>'读取失败']);
        } catch (ErrorException $err_ex) {
            Log::error(get_class($this).' exception '.print_r($err_ex->getMessage(), true));
            return response()->json(['code'=>-2, 'msg'=>'读取失败']);
        }
    }
}
